﻿package game.account.sockets {
	import game.objects.GameObject;
	import game.objects.UserObject;
	import game.debug.Debug;
	import game.errors.GameError;
	import game.objects.AutoObject;
	import game.socket.packages.GETAUTODETAILS;
	import game.socket.SocketHandler;
	import game.socket.Sockets;
	
	public class SocketAuto extends GameObject {
		
		public function SocketAuto():void {
			
			socket.addEventListener ( SocketHandler.RECIVE, socketRecive);
			
		}
		
		public function getDetails(user:UserObject, cid:int = -1):void {
			if (!user)
				user		= config.me;
			
			if (user.autos.length==0) {
				debug ("У пользователя нет машин", Debug.ACCOUNT);
				return;
			}
			
			var auto:AutoObject = user.getAuto(cid);
			if (auto)
				socket.send ( Sockets.LOGIN, GETAUTODETAILS.write(user.id, auto.aid, auto.cid) );
			else
				debug ("Машина не найдена", Debug.ACCOUNT);
		}
		
		private function socketRecive(event:SocketHandler):void {
			if (event.socketPackage is GETAUTODETAILS) {
				var details:GETAUTODETAILS = event.socketPackage;
				if (details.error==0) {
					trace (details);
				} else {
					error(GameError.DATA_ERROR, 2, "", {code: details.error});
				}
			}
		}
	}
	
}